package com.ax.base.drugproducer.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.BuildTree;
import com.ax.base.common.utils.PinYinUtil;
import com.ax.base.drugproducer.dao.BaseDrugClassMapper;
import com.ax.base.drugproducer.domain.BaseDrugClass;
import com.ax.base.drugproducer.service.DrugClassService;
import com.ax.base.system.domain.MenuDO;

@Service("DrugClassService")
public class DrugClassServiceImpl implements DrugClassService {

	@Autowired
	BaseDrugClassMapper bBaseDrugClassMapper;
	
	/**
	 * 药品分类查询(模块:药品)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseDrugClass>
	 */
	@Override
	public List<BaseDrugClass> queryDrugclassSer(Map<String, Object> params){
		List<BaseDrugClass> baseDrugClass = bBaseDrugClassMapper.queryDrugclassMapper(params);
		return baseDrugClass;
	}

	/**
	 * 药品分类增加(模块:药品分类)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseDrugClass baseDrugClass
	 */
	@Override
	public BaseDrugClass getBaseDrugClass(Long pid){
		BaseDrugClass baseDrugClass = bBaseDrugClassMapper.selectByPrimaryKey(pid);
		return baseDrugClass;
	}

	/**
	 * 药品分类增加(模块:药品分类)
	 * 
	 * @param BaseDrugClass baseDrugClass
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertBaseDrugClass(BaseDrugClass baseDrugClass){
		baseDrugClass.setFirstLetter(PinYinUtil.getFirstSpell(baseDrugClass.getDrugClassName()));
		bBaseDrugClassMapper.insertSelective(baseDrugClass);
	}

	/**
	 * 药品分类修改(模块:药品分类)
	 * 
	 * @param BaseDrugClass baseDrugClass
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateBaseDrugClassSer(BaseDrugClass baseDrugClass) {
		baseDrugClass.setFirstLetter(PinYinUtil.getFirstSpell(baseDrugClass.getDrugClassName()));
		bBaseDrugClassMapper.updateByPrimaryKeySelective(baseDrugClass);
		}

	/**
	 * Tree
	 * 
	 * @param Long parentClassId
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getTree(Long parentClassId) {
		String parentId = parentClassId.toString();
		List<Tree<MenuDO>> deps = bBaseDrugClassMapper.listClass();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String id = deps.get(i).getId();
			if (id.equals(parentId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}
	
	/**
	 * 验证类型名称是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean validateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>();
	    String parentClassId = pt.substring(0,pt.indexOf("///"));
		String drugClassName = pt.substring(pt.indexOf("///") + 3);
		params.put("drugClassName",drugClassName);
		params.put("parentClassId",parentClassId);
		boolean validate;
		validate = bBaseDrugClassMapper.validateMapper(params).size() > 0;
		return validate;
	}

	/**
	 * 验证类型名称是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@Override
	public boolean updateValidateSer(String pt) {
		Map<String,Object> params = new HashMap<String,Object>(); 
		String[] pts = pt.split(",");
		params.put("parentClassId",pts[0]);
		params.put("drugClassName",pts[1]);
		params.put("drugClassId",pts[2]);
		boolean validate;
		validate = bBaseDrugClassMapper.validateUpdateMapper(params).size() > 0;
		return validate;
	}

	
}
